package bosc.cn.service.wallet.impl;

import bosc.cn.constant.WalletConstants;
import bosc.cn.dto.common.CommonResponse;
import bosc.cn.dto.common.ResponseWrapper;
import bosc.cn.service.wallet.WalletLimitMaintainService;
import bosc.cn.service.wallet.bo.WalletLimitMaintainReqBO;
import bosc.cn.service.wallet.bo.WalletLimitMaintainResBO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import mapper.WalletInfoMapper;
import model.WalletInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class WalletLimitMaintainServiceImpl implements WalletLimitMaintainService {

    @Autowired
    private WalletInfoMapper walletInfoMapper;

    @Override
    public ResponseWrapper<WalletLimitMaintainResBO> updateWalletLimit(WalletLimitMaintainReqBO reqBO) {

        ResponseWrapper<WalletLimitMaintainResBO> resBO = new ResponseWrapper<>();
        CommonResponse head = new CommonResponse();
        WalletLimitMaintainResBO body = new WalletLimitMaintainResBO();


        try {

            // 正常通信
            head.setRetCd("000000");
            head.setRetMsg("通信成功");
            head.setRetTm(LocalDateTime.now());

            // 钱包ID校验请求参数
            if (reqBO.getWltId() == null || reqBO.getWltId().trim().isEmpty()) {
                body.setPrcSts("PR99");
                body.setPrcMsg("钱包ID不能为空");
                resBO.setHead(head);
                resBO.setBody(body);
                return resBO;
            }

            // 根据钱包ID查找
            WalletInfo walletInfo = walletInfoMapper.selectOne(
                    new LambdaQueryWrapper<WalletInfo>()
                            .eq(WalletInfo::getWltId, reqBO.getWltId())
            );
            if (walletInfo == null) {
                body.setPrcSts("PR88");
                body.setPrcMsg("钱包不存在");
                resBO.setHead(head);
                resBO.setBody(body);
                return resBO;
            }

            // 更新限额字段
            WalletInfo update = new WalletInfo();
            if (reqBO.getWltBalLmt() != null && reqBO.getWltBalLmt().compareTo(WalletConstants.WALLET_BAL_LIMIT) > 0) {
                update.setWltBalLmt(reqBO.getWltBalLmt());
            }
            if (reqBO.getSngLmtAmt() != null && reqBO.getSngLmtAmt().compareTo(WalletConstants.WALLET_SINGLE_LIMIT) > 0) {
                update.setSngLmtAmt(reqBO.getSngLmtAmt());
            }
            if (reqBO.getDlyLmtAmt() != null && reqBO.getDlyLmtAmt().compareTo(WalletConstants.WALLET_DAILY_LIMIT) > 0) {
                update.setDlyLmtAmt(reqBO.getDlyLmtAmt());
            }
            if (reqBO.getYrLmtAmt() != null && reqBO.getYrLmtAmt().compareTo(WalletConstants.WALLET_YEARLY_LIMIT) > 0) {
                update.setYrLmtAmt(reqBO.getYrLmtAmt());
            }

            // 如果没有任何字段需要更新
            if (update.getWltBalLmt() == null && update.getSngLmtAmt() == null
                    && update.getDlyLmtAmt() == null && update.getYrLmtAmt() == null) {
                body.setPrcSts("PR77");
                body.setPrcMsg("输入的限额未超过默认值，未做修改");
                resBO.setHead(head);
                resBO.setBody(body);
                return resBO;
            }

            // 补充更新操作信息
            update.setLastUpdateTime(LocalDateTime.now());
            update.setLastUpdateOperate("钱包限额维护");

            // updateNo：受影响的记录条数
            int updateNo = walletInfoMapper.update(update
                    , new LambdaQueryWrapper<WalletInfo>()
                            .eq(WalletInfo::getWltId, reqBO.getWltId())
            );

            if (updateNo > 0) {
                body.setPrcSts("PR00");
                body.setPrcMsg("钱包限额修改成功");

            } else {
                body.setPrcSts("PR02");
                body.setPrcMsg("钱包限额修改失败");
            }

        } catch (Exception e) {
            // 出现异常，通信失败
            head.setRetCd("999999");
            head.setRetMsg("系统异常：" + e.getMessage());
            head.setRetTm(LocalDateTime.now());
        }

        resBO.setHead(head);
        resBO.setBody(body);
        return resBO;

    }
}
